; *** Chess Swappers
; *** Copyright 1998-2002 Zillions Development
; v.2.0

; You need to purchase Zillions of Games to load this rules file
; Visit the Zillions web site at http://www.zillions-of-games.com
(translate
 ("4 Knight Swap" "4马交换")
 ("6 Knight Swap" "6马交换")
 ("6 Knight Swap II" "6马交换二")
 ("10 Knight Swap" "10马交换")
 ("14 Knight Swap" "14马交换")
 ("24 Knight Swap" "24马交换")
 ("Through the Crowd" "冲阵")
 ("Crossing the Danube" "列交换")
 ("Bishop Swap" "象交换")
 ("Alexander Knight Puzzle" "4马交换（禁送吃）")
)

(define Knight-defs
  (help "马：跳日字，不可吃子")
  (description "马\马跳日字，即横2步竖1步，或横1步竖2步。可越子。不可吃子。")
  (moves
	 (nne (verify empty?) add)
	 (nnw (verify empty?) add)
	 (sse (verify empty?) add)
	 (ssw (verify empty?) add)
	 (ene (verify empty?) add)
	 (ese (verify empty?) add)
	 (wnw (verify empty?) add)
	 (wsw (verify empty?) add)
  )
)

(define check-for-cap
  (if (on-board? nne) (verify (not-piece? $1 nne)))
  (if (on-board? nnw) (verify (not-piece? $1 nnw)))
  (if (on-board? sse) (verify (not-piece? $1 sse)))
  (if (on-board? ssw) (verify (not-piece? $1 ssw)))
  (if (on-board? ene) (verify (not-piece? $1 ene)))
  (if (on-board? ese) (verify (not-piece? $1 ese)))
  (if (on-board? wnw) (verify (not-piece? $1 wnw)))
  (if (on-board? wsw) (verify (not-piece? $1 wsw)))
)

(define non-cap-Knight-defs
  (help "马：跳日字，不可攻击范围吃子，不可走进对方马")
  (description "马\马跳日字，即横2步竖1步，或横1步竖2步。可越子。不可吃子。\本关中，马不得走入对方马的攻击范围。")
  (moves
	 (nne (verify empty?) (check-for-cap) add)
	 (nnw (verify empty?) (check-for-cap) add)
	 (sse (verify empty?) (check-for-cap) add)
	 (ssw (verify empty?) (check-for-cap) add)
	 (ene (verify empty?) (check-for-cap) add)
	 (ese (verify empty?) (check-for-cap) add)
	 (wnw (verify empty?) (check-for-cap) add)
	 (wsw (verify empty?) (check-for-cap) add)
  )
)

(define diagonal
  $2 (verify empty?)
  to
  mark
  (while (empty? $3) $3)
  (if (not-piece? $1 $3)
     back
     (while (empty? $4) $4)
     (if (not-piece? $1 $4) add)
  )
  back
)
(define Bishop-defs
  (help "象：走斜线，格数不限")
  (description "象，本游戏中不吃子")
  (moves
	 ((diagonal $1 ne nw se) (diagonal $1 ne nw se) (diagonal $1 ne nw se))
	 ((diagonal $1 sw nw se) (diagonal $1 sw nw se) (diagonal $1 sw nw se))
	 ((diagonal $1 nw sw ne) (diagonal $1 nw sw ne) (diagonal $1 nw sw ne))
	 ((diagonal $1 se sw ne) (diagonal $1 se sw ne) (diagonal $1 se sw ne))
  )
)


(game
   (title "4 Knight Swap")
   (solution "Solutions\Swappers,_Chess\4_Knight_Swap.zsg")
   (description "目标：将两组马位置交换。")
   (history "This problem, one of the oldest chess puzzles, was posed by Guarini
      di Forli in 1512.")
   (strategy "As Henry E. Dudeney pointed out, the central square will never be
      landed on, while the other squares all have exactly 2 other squares
	  on a Knight's move away.  This puzzle can be solved easily by re-diagraming
	  them: label the squares and draw a line between any two that are
	  a Knight's move away.  The resulting graph can be opened out into a circle,
	  resulting in a topologically isomorphic representation
	  of the problem.\\To see an optimal 16-move solution, select `Show
	  Solution` under the Help menu.")
   (click-sound "Audio\\Pickup.wav")
   (release-sound "Audio\\WoodThunk.wav")
   (players Player)
   (turn-order Player)
   (board
     (image "images\Chess\SHaag\Chess3x3.bmp" "images\Chess\Chess3x3.bmp")
     (grid
       (start-rectangle 5 5 53 53)
       (dimensions
          ("a/b/c" (49 0)) ; files
          ("3/2/1" (0 49)) ; ranks
       )
       (directions (nnw -1 -2) (nne  1 -2) (ene  2 -1) (ese  2  1)
	               (sse  1  2) (ssw -1  2) (wsw -2  1) (wnw -2 -1)
       )
     )
   ) 
   (board-setup
        (Player
		   (WhiteKnight a1 c1)
		   (BlackKnight a3 c3)
		)
   )
   (piece
      (name WhiteKnight)
      (image Player "images\Chess\SHaag\wknight.bmp" "images\Chess\bknight.bmp")
	  (Knight-defs)
   )
   (piece
      (name BlackKnight)
      (image Player "images\Chess\SHaag\bknight.bmp" "images\Chess\bknight.bmp")
	  (Knight-defs)
   )
   (win-condition (Player)
		(and
		   (absolute-config WhiteKnight (a3 c3))
		   (absolute-config BlackKnight (a1 c1))
		)
   )
)

(variant
   (title "6 Knight Swap")
   (default)
   (solution "Solutions\Swappers,_Chess\6_Knight_Swap.zsg")
   (history "This is a simple variation on Guarini's 4 Knight Swap puzzle.")
   (strategy "The solution is only 8 moves long.  To see it,
      select `Show Solution` under the Help menu.")
   (board-setup
        (Player
		   (WhiteKnight a1 b1 c1)
		   (BlackKnight a3 b3 c3)
		)
   )
   (win-condition (Player)
		(and
		   (absolute-config WhiteKnight (a3 b3 c3))
		   (absolute-config BlackKnight (a1 b1 c1))
		)
   )
)

(variant
   (title "6 Knight Swap II")
   (solution "Solutions\Swappers,_Chess\6_Knight_Swap_II.zsg")
   (history "This puzzle, a variation on Guarini's 4 Knight Swap, first published
        in the `Journal of Recreational Mathematics` in 1974.  Martin Gardner analyzed
	  it in `Scientific American` (Dec. 1978, Jan. 1979) and in his book `aha!`")
   (strategy "This puzzle can be solved easily by re-diagraming the board into a graph:
        label the squares and draw a line between any two that are a Knight's move away.
        The resulting graph can be opened out and untangled, resulting in a topologically
        isomorphic representation of the problem.\\
        To see an optimal 16-move solution, select `Show Solution` under the Help menu.")
   (board
     (image "images\Chess\SHaag\Chess3x4.bmp" "images\Chess\Chess3x4.bmp")
     (grid
       (start-rectangle 5 5 53 53)
       (dimensions
          ("a/b/c" (49 0)) ; files
          ("4/3/2/1" (0 49)) ; ranks
       )
       (directions (nnw -1 -2) (nne  1 -2) (ene  2 -1) (ese  2  1)
	               (sse  1  2) (ssw -1  2) (wsw -2  1) (wnw -2 -1)
       )
     )
   ) 
   (board-setup
        (Player
		   (WhiteKnight a1 b1 c1)
		   (BlackKnight a4 b4 c4)
		)
   )
   (win-condition (Player)
		(and
		   (absolute-config WhiteKnight (a4 b4 c4))
		   (absolute-config BlackKnight (a1 b1 c1))
		)
   )
)


(variant
   (title "10 Knight Swap")
   (history "This problem was given by Robert E. Parkin in an article
       published in 1974 in the `Journal of Recreational Mathematics` (7:2).  It
	 is based on the 4 Knight Swap.")
   (strategy "This puzzle can be solved by re-diagraming
	  the board into a graph: label the squares and draw a line between any two that are
	  a Knight's move away.  The resulting graph can be opened out and
	  untangled, resulting in a topologically isomorphic representation
	  of the problem.\\The original published solution was 44 moves.  To see
	  a 42-move solution given by Ted Roth in the `Journal of Recreational
	  Mathematics` (8:4), select `Show Solution` under the Help menu.  Can
	  you do better?")
   (solution "Solutions\Swappers,_Chess\10_Knight_Swap.zsg")
   (board
     (image "images\Chess\SHaag\Chess3x4.bmp" "images\Chess\Chess3x4.bmp")
     (grid
       (start-rectangle 5 5 53 53)
       (dimensions
          ("a/b/c" (49 0)) ; files
          ("4/3/2/1" (0 49)) ; ranks
       )
       (directions (nnw -1 -2) (nne  1 -2) (ene  2 -1) (ese  2  1)
	               (sse  1  2) (ssw -1  2) (wsw -2  1) (wnw -2 -1)
       )
     )
   ) 
   (board-setup
        (Player
		   (WhiteKnight a1 b1 b3 c1 c2)
		   (BlackKnight a4 b2 b4 c3 c4)
 		)
   )
   (win-condition (Player)
		(and
		   (absolute-config WhiteKnight (a4 b2 b4 c3 c4))
		   (absolute-config BlackKnight (a1 b1 b3 c1 c2))
		)
   )
)

(variant
   (title "14 Knight Swap")
   (solution "Solutions\Swappers,_Chess\14_Knight_Swap.zsg")
   (history "This problem was given by Robert E. Parkin in an article
       published in 1974 in the `Journal of Recreational Mathematics` (7:2).  It
	   is based on the 4 Knight Swap.")
   (strategy "This is a difficult problem.  It can be solved by re-diagraming
	  the board into a graph: label the squares and draw a line between any two that are
	  a Knight's move away.  The resulting graph can be opened out and
	  untangled to some extent (it will be nonplanar), resulting in a topologically
	  isomorphic representation of the problem.\\To see the solution 
	  Parkin published, select `Show Solution` under the Help menu.  Can
	  you do better?")
   (board
     (image "images\Chess\SHaag\Chess4x4.bmp" "images\Chess\Chess4x4.bmp")
     (grid
       (start-rectangle 5 5 53 53)
       (dimensions
          ("a/b/c/d" (49 0)) ; files
          ("4/3/2/1" (0 49)) ; ranks
       )
       (directions (nnw -1 -2) (nne  1 -2) (ene  2 -1) (ese  2  1)
	               (sse  1  2) (ssw -1  2) (wsw -2  1) (wnw -2 -1)
       )
     )
   ) 
   (board-setup
        (Player
		   (WhiteKnight a1 a2 b1 c1 c2 d1 d2)
		   (BlackKnight a3 a4 b3 b4 c4 d3 d4)
		)
   )
   (win-condition (Player)
		(and
		   (absolute-config WhiteKnight (a3 a4 b3 b4 c4 d3 d4))
		   (absolute-config BlackKnight (a1 a2 b1 c1 c2 d1 d2))
		)
   )
)

(variant
   (title "24 Knight Swap")
   (solution "Solutions\Swappers,_Chess\24_Knight_Swap.zsg")
   (history "This addictive puzzle appears in Gyles Brandreth's book `Solo Games`
      under the title `Knight Errant`.   It also appears in Katharina Zechlin's
      `Making Games in Wood`, which says it was a popular 19th-century solitaire.")
   (strategy "Don't do more backtracking than necessary; plan so that
      as many moves as possible directly carry Knights towards their
	  goal.  For example, when a White Knight in its original corner
	  location, don't move so that two Black Knights block its way
	  out.  You may find that a good solution has a lot of alternating
	  moves: White, Black, White, Black...\\Gyles Brandreth writes
	  (and Pentagames concurs), `While with practice you should
      finish the game within 50 moves, it is almost impossible to do it
	  in less than 45.`  To see the 36-move solution Jeff Mallett came up with,
	  select `Show Solution` under the Help menu.  (He's still
	  working on a 34-move one!)")
   (board
     (image "images\Chess\SHaag\Chess5x5.bmp" "images\Chess\Chess5x5.bmp")
     (grid
       (start-rectangle 5 5 53 53)
       (dimensions
          ("a/b/c/d/e" (49 0)) ; files
          ("5/4/3/2/1" (0 49)) ; ranks
       )
       (directions (nnw -1 -2) (nne  1 -2) (ene  2 -1) (ese  2  1)
	               (sse  1  2) (ssw -1  2) (wsw -2  1) (wnw -2 -1)
       )
     )
   ) 
   (board-setup
        (Player
		   (WhiteKnight a1 b1 c1 d1 e1 a2 b2 c2 d2 a3 b3 a4)
		   (BlackKnight a5 b5 c5 d5 e5 b4 c4 d4 e4 d3 e3 e2)
		)
   )
   (win-condition (Player)
		(and
		   (absolute-config WhiteKnight (a5 b5 c5 d5 e5 b4 c4 d4 e4 d3 e3 e2))
		   (absolute-config BlackKnight (a1 b1 c1 d1 e1 a2 b2 c2 d2 a3 b3 a4))
		)
   )
)

(variant (title "-")) ;------------------------------------------------------


(variant
   (title "Through the Crowd")
   (history "")
   (strategy "Figure out a path from the White Knight's starting square to
      its goal (this should only be four steps).    For each step, you need
      to swap around several Black Knights to vacate the square where you
      want the White Knight to go next.\\
      The solution is 17 moves long.  To see it, select `Show Solution` under
	  the Help menu.")
   (description "通过调动，将白马移右上角的位置。")
   (solution "Solutions\Swappers,_Chess\Through_The_Crowd.zsg")
   (board
     (image "images\Chess\SHaag\Chess5x5.bmp" "images\Chess\Chess5x5.bmp")
     (grid
       (start-rectangle 5 5 53 53)
       (dimensions
          ("a/b/c/d/e" (49 0)) ; files
          ("5/4/3/2/1" (0 49)) ; ranks
       )
       (directions (nnw -1 -2) (nne  1 -2) (ene  2 -1) (ese  2  1)
	               (sse  1  2) (ssw -1  2) (wsw -2  1) (wnw -2 -1)
       )
     )
   ) 
   (board-setup
        (Player
		   (WhiteKnight a1)
		   (BlackKnight a5 b5 c5 d5 b4 c4 d4 e4 d3 e3 e2
		       b1 c1 d1 e1 a2 b2 c2 d2 a3 b3 c3 a4)
		)
   )
   (win-condition (Player)
		   (absolute-config WhiteKnight (e5))
   )
)


(define scan-file
   (while (empty? $1) $1) (verify (not-friend? $1)) back
)
(define scan-move
   ($1 (verify empty?) mark (scan-file n) (scan-file s) add)
)
(variant
   (title "Crossing the Danube")
   (description "将白马移到左边，黑马移到右边。两组马均不能后撤，即白马不能往右，黑马不能往左。并且，任何时候两个马不能处于同一条竖线。")
   (history "This problem by Sam Loyd appeared in `Cleveland Voice`
      on July 1, 1877.  Loyd considered this one of his most difficult problems.")
   (strategy "This puzzle is equivalent to the linear Swappers puzzles with marbles.  You
      must avoid setting up an impasse of two similarly colored Knights on
	  adjacent columns.  To see a solution, select `Show Solution` under the Help menu.")
   (solution "Solutions\Swappers,_Chess\Crossing_the_Danube.zsg")
   (board
     (image "images\Chess\SHaag\Chess8x8.bmp" "images\Chess\Chess8x8.bmp")
     (grid
       (start-rectangle 5 5 53 53)
       (dimensions
          ("a/b/c/d/e/f/g/h" (49 0)) ; files
          ("8/7/6/5/4/3/2/1" (0 49)) ; ranks
       )
       (directions (nnw -1 -2) (nne  1 -2) (ene  2 -1) (ese  2  1)
	               (sse  1  2) (ssw -1  2) (wsw -2  1) (wnw -2 -1)
				   (n 0 -1) (s 0 1)
       )
     )
     (zone (name a-file) (players Player) (positions a1 a2 a3 a4 a5 a6 a7 a8))
     (zone (name b-file) (players Player) (positions b1 b2 b3 b4 b5 b6 b7 b8))
     (zone (name c-file) (players Player) (positions c1 c2 c3 c4 c5 c6 c7 c8))
     (zone (name e-file) (players Player) (positions e1 e2 e3 e4 e5 e6 e7 e8))
     (zone (name f-file) (players Player) (positions f1 f2 f3 f4 f5 f6 f7 f8))
     (zone (name g-file) (players Player) (positions g1 g2 g3 g4 g5 g6 g7 g8))
     (zone (name h-file) (players Player) (positions h1 h2 h3 h4 h5 h6 h7 h8))
  ) 
   (piece
      (name WhiteKnight)
	  (help "白马，只能向左跳")
	  (description "Knight\A Knight moves like an `L`, two squares vertically plus one
	     horizontally, or two squares horizontally plus one vertically.  It hops over anything
		 on the way.  There is no capturing in this puzzle.  A White Knight can only move
		 towards the left.")
      (image Player "images\Chess\SHaag\wknight.bmp" "images\Chess\wknight.bmp")
	  (moves
		 (scan-move nnw)
		 (scan-move ssw)
		 (scan-move wnw)
		 (scan-move wsw)
	  )
   )
   (piece
      (name BlackKnight)
	  (help "黑马：只能向右跳")
	  (description "Knight\A Knight moves like an `L`, two squares vertically plus one
	     horizontally, or two squares horizontally plus one vertically.  It hops over anything
		 on the way.  There is no capturing in this puzzle.  A Black Knight can only move
		 towards the right.")
      (image Player "images\Chess\SHaag\bknight.bmp" "images\Chess\bknight.bmp")
	  (moves
		 (scan-move nne)
		 (scan-move sse)
		 (scan-move ene)
		 (scan-move ese)
	  )
   )
   (board-setup
        (Player
		   (WhiteKnight f5 g5 h5)
		   (BlackKnight a5 b5 c5 d5)
		)
   )
   (win-condition (Player)
		(and
		   (absolute-config WhiteKnight (a-file b-file c-file))
		   (absolute-config BlackKnight (e-file f-file g-file h-file))
		)
   )
   (loss-condition (Player) stalemated)
)

(variant (title "-")) ;------------------------------------------------------

(variant
   (title "Bishop Swap")
   (description "目标：将两组象位置交换，且任何时候黑白象不得处于同一斜线。")
   (history "The given solution can be found in Maxey Brooke's 1963 book
     `Coin Games and Puzzles`, and in L. E. Hordern's `Sliding Piece
     Puzzles.`  The puzzle originated with the puzzle king H. E. Dudeney, who
     published it as `A New Bishop's Puzzle` in his book `Amusements in Mathematics`
     in 1917.   This puzzle has also shown up in the popular program `The 7th Guest.`")
   (strategy "This is really the same problem repeated twice, because
      exchanging the dark-squared Bishops is analogous to exchanging
	  the light-squared Bishops.  To see a 36-move solution,
	  select `Show Solution` under the Help menu.")
   (solution "Solutions\Swappers,_Chess\Bishop_Swap.zsg")
   (board
     (image "images\Chess\SHaag\Chess4x5.bmp" "images\Chess\Chess4x5.bmp")
     (grid
       (start-rectangle 5 5 53 53)
       (dimensions
          ("a/b/c/d" (49 0)) ; files
          ("5/4/3/2/1" (0 49)) ; ranks
       )
       (directions (ne 1 -1) (nw -1 -1) (se 1 1) (sw -1 1) )
     )
   )
   (piece (name WhiteKnight))
   (piece (name BlackKnight))
   (piece
        (name WhiteBishop)
        (image Player "images\Chess\SHaag\wbishop.bmp" "images\Chess\wbishop.bmp")
		(Bishop-defs BlackBishop)
   )
   (piece
        (name BlackBishop)
        (image Player "images\Chess\SHaag\bbishop.bmp" "images\Chess\bbishop.bmp")
		(Bishop-defs WhiteBishop)
   )
   (board-setup
        (Player
		   (WhiteBishop a1 b1 c1 d1)
		   (BlackBishop a5 b5 c5 d5)
		)
   )
   (win-condition (Player)
		(and
		   (absolute-config WhiteBishop (a5 b5 c5 d5))
		   (absolute-config BlackBishop (a1 b1 c1 d1))
		)
   )
)

(variant
   (title "Alexander Knight Puzzle")
   (history "W. Alex Doskey <http://www.doskey.com> came up with this derivative
      of a similar (but easier) puzzle in the computer game `The 7th Guest` by using
      Knights rather than Bishops.  The original puzzle appears here as
      `Bishop Swap`.")
   (strategy "The shortest solution is 32 moves long.  To see it,
      select `Show Solution` under the Help menu.")
   (description "将两组马位置交换。不得走入对方马的攻击范围。")
   (solution "Solutions\Swappers,_Chess\Four_By_Five.zsg")
   (board
     (image "images\Chess\SHaag\Chess4x5.bmp" "images\Chess\Chess4x5.bmp")
     (grid
       (start-rectangle 5 5 53 53)
       (dimensions
          ("a/b/c/d" (49 0)) ; files
          ("5/4/3/2/1" (0 49)) ; ranks
       )
       (directions (nnw -1 -2) (nne  1 -2) (ene  2 -1) (ese  2  1)
	               (sse  1  2) (ssw -1  2) (wsw -2  1) (wnw -2 -1)
       )
     )
   ) 
   (piece
      (name WhiteKnight)
      (image Player "images\Chess\SHaag\wknight.bmp" "images\Chess\wknight.bmp")
	  (non-cap-Knight-defs BlackKnight)
   )
   (piece
      (name BlackKnight)
      (image Player "images\Chess\SHaag\bknight.bmp" "images\Chess\bknight.bmp")
	  (non-cap-Knight-defs WhiteKnight)
   )
   (board-setup
        (Player
		   (WhiteKnight a1 b1 c1 d1)
		   (BlackKnight a5 b5 c5 d5)
		)
   )
   (win-condition (Player)
        (and 
		   (absolute-config WhiteKnight (a5 b5 c5 d5))
		   (absolute-config BlackKnight (a1 b1 c1 d1))
		)
   )
)

